<%@page
	import="com.neiquan.backstage.platform_alliance_member.PlatformAllianceMemberService"%>
<%@page
	import="com.neiquan.backstage.pub.bsc.dao.po.PlatformAllianceMember"%>
<%@page import="java.util.List"%>
<%@page import="com.neiquan.backstage.common.CommonService"%>
<%@page import="com.neiquan.backstage.common.util.SpringContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>租金缴纳</title>
<link rel="stylesheet" href="css/base.css" />
<link rel="stylesheet" href="css/main.css" />

</head>
<body bgcolor="#f0f0f0">
	<section class="mt tc">
		<section id="noStation" class="common_bg mt">
			<section class="perfect pd">
				<p class="form-inpt-p">
					<label>当前暂无工位信息</label>
				</p>
			</section>
			<section class="addr_sub pd">
				<input type="button" class="btn rent_submmit"
					onClick="gotoApplyPage()" value="申请工位">
			</section>
		</section>
		<section id="rental">
			<p class="rent_top pd">
				目前已缴费截止月份
				<time id="endTime" class="rent_time"></time>
			</p>
			<p id="noArrearage" class="rent_top pd">
				已欠费<span id="numberMonth"></span>个月<span>金额</span>
				<time id="arrearage" class="rent_time"></time>
				元
			</p>
			<section class="pd" style="height:auto;background:#fff;">
				<section class="rent_bottom pd cb">
					<label class="rent-label">缴费</label>
					<section class="relative rent-input-wrap">
						<input class="month_inpt" type="text" placeholder="请选择" />个月 （<span
							id="sumRental"></span>元/月）<br>
						<time class="rent_tips">
							费用缴纳至<span id="endTime3"></span>
						</time>
						<ul class="rent_date" style="position: absolute;">
							<li>
								<span onClick="addmulMonth(1)">1</span> 
								<span onClick="addmulMonth(5)">5</span>
								<span onClick="addmulMonth(9)">9</span>
							</li>
							<li>
							<span onClick="addmulMonth(2)">2</span> 
							<span onClick="addmulMonth(6)">6</span> 
							<span onClick="addmulMonth(10)">10</span>
							</li>
							<li>
							<span onClick="addmulMonth(3)">3</span> 
							<span onClick="addmulMonth(7)">7</span>
							 <span onClick="addmulMonth(11)">11</span>
							</li>
							<li><span onClick="addmulMonth(4)">4</span> 
							<span onClick="addmulMonth(8)">8</span>
							 <span onClick="addmulMonth(12)">12</span>
							</li>
						</ul>
					</section>
				</section>
				<p style="font-size: 0.5em;">
					合计金额
					<time id="totalPrice" class="rent_num"></time>
					元
				</p>
				<section class="rent_btn relative">
					<input type="button" class="btn rent_submmit cp" value="立即缴费" onClick="wxPay()">
					<a href="mystation.jsp" class="btn rent_submmit look_station">查看工位</a>
					<a href="rentnonerecord.jsp" class="btn rent_submmit rent_record">历史缴费记录</a>
				</section>
			</section>
			
		</section>
		<section id="newly">
			<p class="rent_top pd">说明：因您有新申请工位，您需要先缴纳新工位的租金，缴纳的结束时间与您的上一批工位租金缴纳结束时间一致</p>
			<p style="font-size: 0.5em;">
				你需缴费
				<time id="needPayment" class="rent_num"></time>
				元
			</p>
			<time class="rent_tips">
				费用缴纳至<span id="endTime2"></span>
			</time>
			<section class="rent_btn relative">
				<input type="button" onClick="wxPay()" class="btn rent_submmit" value="立即缴费">
				<a href="mystation.jsp" class="btn rent_submmit look_station">查看工位</a>
				<a href="rentnonerecord.jsp" class="btn rent_submmit rent_record">历史缴费记录</a>
			</section>
		</section>
	</section>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/main.js"></script>
<script src="js/responsive.js"></script>
<script src=" https://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script>

<script type="text/javascript">
	var arrearage=0;
	var sumRental=0;
	var endTime="";
	var endTime1="";
	var numberMonth=0;
	var totalPrice=0;
	var leaseId=0;
	//微信支付数据
	var appId; 		//公众号名称，由商户传入     
	var timeStamp; 	//时间戳，自1970年以来的秒数     
	var nonceStr; 	//随机串     
	var packAge;
	var signType; 	//微信签名方式:     
	var paySign;
	function wxPay(){
		var reqDate={
				"totalPrice":totalPrice,
				"body":"工位缴费",
				"monthNumber":numberMonth,
				"endTime":endTime1,
				"leaseId":leaseId,
		}
		$.post("api/paymentAction/wxPay",reqDate,function(data){
			appId=data.response.appId; //公众号名称，由商户传入     
			timeStamp=data.response.timeStamp; //时间戳，自1970年以来的秒数     
			nonceStr=data.response.nonceStr; //随机串     
			packAge=data.response.package;
			signType=data.response.signType; //微信签名方式:     
			paySign=data.response.paySign;
			   WeixinJSBridge.invoke(
				       'getBrandWCPayRequest', {
				           "appId":appId,  
				           "timeStamp":timeStamp,        
				           "nonceStr" : nonceStr, 
				           "package": packAge,     
				           "signType":signType,  
				           "paySign":paySign 
				       },
				       function(res){     
				           if(res.err_msg == "get_brand_wcpay_request：ok" ) {
				        	   aletr("付款成功");
				           }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
				       }
				   );
		});
	};
	
	function onBridgeReady(){
	}
		if (typeof WeixinJSBridge == "undefined"){
		   if( document.addEventListener ){
		       document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
		   }else if (document.attachEvent){
		       document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
		       document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		   }
		}else{
		   onBridgeReady();
		}  
	
	$(function() {
		$("#noStation").hide();
		$("#rental").hide();
		$("#newly").hide();
	});
	//获取全部信息判断显示
	$(function() {
		var url = "api/paymentAction/allInfo";
		$.post(url, function(data) {
			var listL = data.response.listL;
			var listP = data.response.listP;
			//有无租单表判断有无工位
			if (listL.length <= 0) {
				//无工位
				$("#noStation").show();
			} else if (listP.length <= 0) {
				for(i=0;i<listL.length;i++){
					if(listL[i].newlyStuats==0&&listL[i].status!=1){
						$("#noStation").show();
						return;
					}
				}
					//判断是否第一次付费通过有无付款单判断
				var myTime = 0;
				for (i = 0; i < listL.length; i++) {
					if (listL[i].newlyStuats == 0 && listL[i].status == 1) {
						leaseId=listL[i].leaseId;
						sumRental = listL[i].sumRental;
						myTime = listL[i].stratTime;
						fagle1=true;
						break;
					}
				}
				var time = new Date();
				time.setTime(myTime);
				var year = time.getFullYear();
				var month = (time.getMonth()+1) + "";
				var day = time.getDate() + "";
				if (month.length == 1) {
					month = "0" + month;
				}
				if (day.length == 1) {
					day = "0" + day;
				}
				if(month==00){
					month=12;
					year=year-1;
				}
				endTime = year + "-" + month + "-" + day;
				$("#endTime").text(endTime);
				$("#sumRental").text(sumRental);
				$("#rental").show();
				$.post("api/paymentAction/firstPayment", function(data1) {
					arrearage = data1.response.arrearage;
					var numberMonth1 = data1.response.numberMonth;
					$("#arrearage").text(arrearage);
					$("#numberMonth").text(numberMonth1);
				});
			} else {
				var falge1 = false;
				for (i = 0; i < listL.length; i++) {
					//判断是否有新增工位
					if (listL[i].newlyStuats == 1 && listL[i].status == 1) {
						leaseId = listL[i].leaseId;
						sumRental = listL[i].sumRental;
						falge1 = true;
						break;
					}
				}
				if (falge1) {
					$("#newly").show();
					$.post("api/paymentAction/findPayment", {
						"leaseId" : leaseId
					}, function(data2) {
						totalPrice = data2.response.needPayment;
						var endTime3 = data2.response.endTime1;
						var time = new Date();
						time.setTime(endTime3);
						var year = time.getFullYear();
						var month = (time.getMonth()+1) + "";
						var day = time.getDate() + "";
						if (month.length == 1) {
							month = "0" + month;
						}
						if (day.length == 1) {
							day = "0" + day;
						}
						if(month==00){
							month=12;
							year=year-1;
						}
						var endTime2 = year + "-" + month + "-" + day;
						endTime1=endTime2;
						numberMonth=0;
						$("#needPayment").text(totalPrice);
						$("#endTime2").text(endTime2);
					});
				}else{
					$("#rental").show();
					var falge2=false;
					for (i = 0; i < listL.length; i++) {
						if (listL[i].newlyStuats == 0 && listL[i].status == 1) {
							leaseId = listL[i].leaseId;
							sumRental = listL[i].sumRental;
							$("#sumRental").text(sumRental);
							falge2 = true;
							break;
						}
					}
					if(falge2){
						$.post("api/paymentAction/firstPayment", function(data1) {
							var endTime1=data1.response.endTime;
							var myTime=new Date();
							myTime.setTime(endTime1);
							var year = myTime.getFullYear();
							var month = (myTime.getMonth()+1) + "";
							var day = myTime.getDate() + "";
							if (month.length == 1) {
								month = "0" + month;
							}
							if (day.length == 1) {
								day = "0" + day;
							}
							if(month==00){
								month=12;
								year=year-1;
							}
							endTime = year + "-" + month + "-" + day;
							$("#endTime").text(endTime);
							numberMonth = data1.response.numberMonth;
							arrearage = data1.response.arrearage;
							if(arrearage<=0){
								$("#noArrearage").hide();
							}
							$("#arrearage").text(arrearage);
							$("#numberMonth").text(numberMonth);
						});
					}
				}
			}
		});
	});
	//年月日相加 和需要的总价格
	function addmulMonth(numMonth) {
		// n个月后
		numberMonth= parseInt(numMonth);
		var numMonth = parseInt(numMonth);
		//微信不好使
		/* var time = new Date(Date.parse(str.replace(/-0/g, "-"))); */
		var time1 = new Date();
		var b=parseFloat(arrearage);
		if(b<=0){
			var str = endTime;
			var spl=str.split("-");
			var year1 =spl[0];
			var month1=spl[1];
			var day1=spl[2];
			var time = new Date(year1, month1, day1);
			time.setMonth(time.getMonth() + (numMonth))
			time1.setTime(time);
		}else{
			var time = new Date();
			time1.setMonth(time.getMonth() + (numMonth+1))
		}
		var year = time1.getFullYear();
		var month = time1.getMonth() + "";
		var day = time1.getDate() + "";
		if (month.length == 1) {
			month = "0" + month;
		}
		if (day.length == 1) {
			day = "0" + day;
		}
		if(month==00){
			month=12;
			year=year-1;
		}
		endTime1 = year + "-" + month + "-" + day;
		//计算总花费
		sumRental = parseInt(sumRental);
		totalPrice = ((sumRental * numMonth) + parseFloat(arrearage)).toFixed(2);
		$("#totalPrice").text(totalPrice);
		$("#endTime3").text(endTime1);
	}
	
	function gotoApplyPage(){
		window.location.href="mystation.jsp";
	}
</script>
</html>
